jQuery(function($) {
	
	// 切换常用收货地址
	$('#address_list input[type="radio"][name="addressid"]').live('change', function(){
		var addressid = $(this).val();
		get_address(addressid);
	});

	$('#save_address').click(function(){
		save_address(this);
		return false;
	});

	$('#fm_address input, #fm_address select').change(function(){
		$('#address_list input[type="radio"][name="addressid"]').attr('checked', false);
	});

	// 支付方式，配送方式
	$('input[type="radio"][name="payment"],input[type="radio"][name="delivery"]').change(function(){
		var payment = $('input[type="radio"][name="payment"]:checked').val();
		var delivery = $('input[type="radio"][name="delivery"]:checked').val();
		var opts = {
			'payment'	: '',
			'delivery'	: '',
			'gifts'		: ''
		};
		var param = $.extend({}, opts, {'payment' : payment, 'delivery' : delivery});
		$.getJSON('./index.php?frm=ajax&mdl=flow&do=cal_amount', param, function(json){
			$('#cost').text(json.cost);
			$('#payment_fee').text(json.payment_fee);
			$('#delivery_fee').text(json.delivery_fee);
			$('input[type="hidden"][name="payment_fee"]').val(json.payment_fee);
			$('input[type="hidden"][name="delivery_fee"]').val(json.delivery_fee);
			$('input[type="hidden"][name="cost"]').val(json.cost);
		});
	});

	// 表单提交判断
	$('form#fm_checkout').bind('submit', function(evt){
		var errmsg = [];
		if($('input[type="radio"][name="payment"]:checked').length == 0){
			errmsg.push('未选择支付方式');
		}
		if($('input[type="radio"][name="delivery"]:checked').length == 0){
			errmsg.push('未选择配送方式');
		}
		if(errmsg.length > 0){
			errmsg = errmsg.join('<br />');
			showmsg_warning(errmsg);
			return false;
		}
		return true;
	});
});


/*******************************************************************************
 * 收货地址
 *******************************************************************************/

function get_address_from_editor(){
	var fm = $('#fm_address');
	var address = {
		province_id	: $(fm).find('select[name="province_id"]').val(),
		city_id		: $(fm).find('select[name="city_id"]').val(),
		area_id		: $(fm).find('select[name="area_id"]').val(),
		address		: $.trim($(fm).find('input[name="address"]').val()),
		consignee	: $.trim($(fm).find('input[name="consignee"]').val()),
		zipcode		: $.trim($(fm).find('input[name="zipcode"]').val()),
		mobile		: $.trim($(fm).find('input[name="mobile"]').val()),
		tel			: $.trim($(fm).find('input[name="tel"]').val()),
		email		: $.trim($(fm).find('input[name="email"]').val()),
		besttime	: $.trim($(fm).find('select[name="besttime"]').val())
	}
	var errmsg = [];
	if(address.province_id == '')errmsg.push('未选择：省');
	if(address.city_id == '')errmsg.push('未选择：市');
	if(address.area_id == '')errmsg.push('未选择：区');
	if(address.consignee == '')errmsg.push('未输入：收货人姓名');
	if(address.address == '')errmsg.push('未输入：详细收货地址');
	if(address.mobile == ''){
		if(address.tel == '')errmsg.push('未输入：手机');
	}
	address['errmsg'] = errmsg;
	return address;
}

// 添加到常用地址
function save_address(obj) {
	var fm = $('#fm_address');
	var address = get_address_from_editor();	
	if(address.errmsg.length > 0){
		errmsg = address.errmsg.join('<br />');
		showmsg_warning(errmsg);
		return false;
	}
	jQuery.post('./index.php?frm=ajax&mdl=address&do=save', address, function(data, textStatus, xhr){
		json = $.json.decode(data);
		if(json.result == 'succ'){
			var province_name = $.trim($(fm).find('select[name="province_id"]').find('option:selected').text());
			var city_name = $.trim($(fm).find('select[name="city_id"]').find('option:selected').text());
			var area_name = $.trim($(fm).find('select[name="area_id"]').find('option:selected').text());
			$('#address_list').append('<dd><input type="radio" name="addressid" value="'+json.id+'" />'+address.consignee+'-'+province_name+','+city_name+','+area_name+','+address.address+'，'+address.zipcode+'，'+address.mobile+'</dd>').parent().show();
		}else{
			showmsg_warning(json.errmsg);
		}
	});
}

function get_address(addressid){
	showmsg_loading('地址切换中...', {title:'AJAX'});
	$.getJSON('./index.php?frm=ajax&mdl=address&do=get', {'addressid' : addressid}, function(json, textStatus, xhr){
		if(json.result == 'succ'){
			var fm = $('#fm_address');
			for(var x in json.address){
				if(x == 'province_id' || x == 'city_id' || x == 'area_id'){
					if(x == 'province_id'){
						$(fm).find('select[name="'+x+'"]').val(json.address[x]);
					}
					if(x == 'city_id'){
						$(fm).find('select[name="'+x+'"]').html('<option value="">--市--</option>'+json.address[x]);
					}
					if(x == 'area_id'){
						$(fm).find('select[name="'+x+'"]').html('<option value="">--区--</option>'+json.address[x]);
					}

				}else if(x == 'besttime'){
					$(fm).find('select[name="'+x+'"]').val(json.address[x]);
				}else{
					$(fm).find('input[name="'+x+'"]').val(json.address[x]);
				}
			}

		}else{
			showmsg_error(json.errmsg);
		}
		$.colorbox.close();	// 关闭弹出层
	});
}